<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>课程管理</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../include/commonFile.jsp" %>
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <div class="layui-tab-item layui-show">
                <div class="layui-tab layui-tab-brief" lay-filter="type">
                    <ul class="layui-tab-title" id="zc_tab">
                        <li <c:if test="${showAll == true}">class="layui-this"</c:if>>全部课程</li>
                        <li <c:if test="${showAll == false}">class="layui-this"</c:if>>分销课程</li>
                    </ul>
                    <div class="layui-tab-content">
                        <form class="layui-form" action="${ctx}/activity/course/courseList.do" id="myForm" method="post">
                            <input type="hidden" name="pageNo" id="pageNo"/>
                            <div class="f-search-bar">
                                <div class="search-container">
                                    <ul class="search-form-content">
                                        <li class="form-item-inline"><label class="search-form-lable">课程名称</label>
                                            <div class="layui-input-inline">
                                                <input type="text" name="title" autocomplete="off" class="layui-input"
                                                       value="${activity.title}" placeholder="请输入课程名称"
                                                >
                                            </div>
                                        </li>
                                        <li class="form-item-inline">
                                            <label class="search-form-lable">项目状态</label>
                                            <div class="layui-input-inline" style="width: 165px">
                                                <select name="timeStatus">
                                                    <option value="">全部</option>
                                                    <option value="0" ${input.timeStatus == 0 ? 'selected' : ''}>进行中</option>
                                                    <option value="1" ${input.timeStatus == 1 ? 'selected' : ''}>已截止</option>
                                                </select>
                                            </div>
                                        </li>
                                        <li class="form-item-inline" style="margin-left: -55px;"><label class="search-form-lable">分销状态</label>
                                            <div class="layui-input-inline">
                                                <select name="isDistribution">
                                                    <option value="">全部</option>
                                                    <option value="1" ${activity.isDistribution == 1 ? 'selected="selected"' : ''}>分销中</option>
                                                    <option value="0" ${activity.isDistribution == 0 ? 'selected="selected"' : ''}>不可分销</option>
                                                </select>
                                            </div>
                                        </li>
                                    </ul>
                                    <ul class="search-form-content">
                                        <li class="form-item"><label class="search-form-lable">开始时间</label>
                                            <div class="layui-inline">
                                                <div class="layui-input-inline">
                                                    <input class="layui-input" type="text" name="startBegin"
                                                           value="${startBegin}" placeholder="开始日"
                                                    >
                                                </div>
                                                -
                                                <div class="layui-input-inline">
                                                    <input class="layui-input" type="text" name="startEnd"
                                                           value="${startEnd}" placeholder="截止日"
                                                    >
                                                </div>
                                            </div>
                                            <div class="cl"></div>
                                        </li>
                                        <li class="form-item-inline">
                                            <div class="sub-btns">
                                                <a class="layui-btn layui-btn-danger"
                                                   href="javascript:submitFunction('#myForm')">查询</a>
                                                <a class="layui-btn layui-btn-normal"
                                                   href="javascript:resetFunction('#myForm')">重置</a>
                                            </div>
                                        </li>
                                    </ul>
                                    <ul class="search-form-content">
                                        <li class="form-item"><label class="search-form-lable">发布时间</label>
                                            <div class="check-btn-inner" id="timeType">
                                                <a id="all" href="javascript:void(0);"
                                                   onclick="setTimeType($(this),0,'#myForm')" ${empty input.timeType || input.timeType == 0 ? 'class="active"' : ''}>全部</a>
                                                <a href="javascript:void(0);"
                                                   onclick="setTimeType($(this),1,'#myForm')" ${input.timeType == 1 ? 'class="active"' : ''}>今天</a>
                                                <a href="javascript:void(0);"
                                                   onclick="setTimeType($(this),2,'#myForm')" ${input.timeType == 2 ? 'class="active"' : ''}>本周内</a>
                                                <a href="javascript:void(0);"
                                                   onclick="setTimeType($(this),3,'#myForm')" ${input.timeType == 3 ? 'class="active"' : ''}>本月内</a>
                                                <input type="hidden" name="timeType" value="${input.timeType}"/>
                                            </div>
                                            <div class="layui-inline">
                                                <div class="layui-input-inline">
                                                    <input class="layui-input" type="text" name="createStart"
                                                           value="${input.createStart}" placeholder="开始日">
                                                </div>
                                                -
                                                <div class="layui-input-inline">
                                                    <input class="layui-input" type="text" name="createEnd"
                                                           value="${input.createEnd}" placeholder="截止日">
                                                </div>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </form>
                        <div class="c-time-list-content">
                            <ul id="view">
                                <c:if test="${page.totalCount == 0}">
                                    <div class="f16 tc mt15">还没有数据，请点击右上角的“发布课程”，发布和编辑内容</div>
                                </c:if>
                                <c:set var="nowDate" value="<%=new Date()%>"></c:set>
                                <c:forEach var="activity" items="${activities}">
                                    <li>
                                        <div class="detail-content">
                                            <a href="javascript:void(0)" class="act-logo common-img">
                                                <img src="<c:if test="${not empty activity.pic}">${activity.pic}?imageMogr2/auto-orient/crop/200x150</c:if>"
                                                     alt="" onerror="txz.errImg(this)" onload="txz.imgLoad(this)">
                                            </a>
                                            <div class="detail">
                                                <div class="act-title">
                                                    <c:if test="${activity.endTime > nowDate}">
                                                        <span class="status f18 red">进行中</span>
                                                    </c:if>
                                                    <c:if test="${activity.endTime < nowDate}">
                                                        <span class="status f18 dark">已截止</span>
                                                    </c:if>
                                                    <a title="${activity.title}" class="title f18 ell db" href="javascript:void(0)">${activity.title}</a>
                                                </div>
                                                <div>
                                                    <p class="act-price">
                                                        <span style="margin-right: 10px;"> 活动开始时间：
                                                            <fmt:formatDate value="${activity.startTime}" pattern="yyyy-MM-dd HH:mm"/></span>
                                                        <span> 报名截止时间：
                                                            <fmt:formatDate value="${activity.endTime}" pattern="yyyy-MM-dd HH:mm"/></span>
                                                    </p>
                                                    <p class="act-price">
                                                        <span style="margin-right: 10px;">报名费用：
                                                            <c:if test="${activity.price == 0.0}">
                                                                <b class="active-red">免费</b>
                                                            </c:if>
                                                            <c:if test="${activity.price > 0.0}">
                                                                <b class="active-red">${activity.price}</b>元/位
                                                            </c:if>
                                                        </span>
                                                        <span class="mr10">限制人数：
															<c:if test="${activity.limitNum == 0}">
                                                                <b class="active-red">不限</b>
                                                            </c:if>
															<c:if test="${activity.limitNum > 0}">
                                                                <b class="active-red">${activity.limitNum }</b>人
                                                            </c:if>
														</span>
                                                        <span style="margin-right: 10px;">报名人数：<b class="active-red">${activity.joinNum}</b>人</span>
                                                    </p>
                                                    <p class="start-time">活动地点：${activity.area} ${activity.place}</p>
                                                    <p class="act-price">
                                                        发布时间：
                                                        <fmt:formatDate value="${activity.createDate}" pattern="yyyy-MM-dd HH:mm"/>
                                                    </p>
                                                </div>
                                                <div class="opts-btns tb-opts" style="width:475px">
                                                    <div class="comm-opts">
                                                        <a class="qr-btn"
                                                           href="javascript:txz.openQrDialog({
                                                               title:'扫码二维码可预览分享',
                                                               detailList:[
                                                                   {
                                                                   title:'报名二维码',
                                                                   url:'${activity.qrCodeUrl }'
                                                                   }
                                                               ]
                                                               })"><i
                                                                class="iconfont icon-qrcode btn-icon"></i> 二维码</a>
                                                        <a href="${ctx}/activity/course/applyList.do?actId=${activity.id}">
                                                            <i class="iconfont icon-peoplelist btn-icon"></i>
                                                            报名管理</a>
                                                        <%--<a href="${ctx}/notify/template/activity/list.do?targetId=${activity.id}">
                                                            <i class="iconfont icon-mobile1 btn-icon"></i>
                                                            短信推送
                                                        </a>--%>
                                                        <c:if test="${activity.joinNum>=0 && activity.endTime>=nowDate}">
                                                            <a href="javascript:setDistribution('${activity.id}', '${activity.disCount}');"
                                                               style="color: #6f5499;">设置分销</a>
                                                        </c:if>
                                                        <a href="${ctx}/activity/course/courseForm.do?activityId=${activity.id}">
                                                            <i class="iconfont icon-edit btn-icon"></i> 编辑</a>
                                                        <a href="javascript:txz.deleteObject('确定要删除吗？','${ctx}/activity/activity/deleteLogic.do?id=${activity.id}','该活动已有人报名，不能删除');">
                                                            <i class="iconfont icon-delete btn-icon"></i> 删除
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </li>
                                </c:forEach>
                            </ul>
                            <div id="page_content" class="page-container"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
    <%--设置可分销弹窗--%>
    <div id="distributionDiv" style="padding: 0px 10px">
        <div class="layui-form" lay-filter="distribution">
            <form id="distributionForm" class="layui-form mt20" method="post" action="${ctx}/activity/activity/setDistribution.do">
                <input type="hidden" name="activityId" value=""/>

                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label">分销状态</label>
                        <div class="layui-input-inline" style="width: 250px" >
                            <input type="radio" name="isDistribution" value="1"
                                   title="可分销" lay-filter="isDistribution" />
                            <input type="radio" name="isDistribution" value="0"
                                   title="不可分销" lay-filter="isDistribution" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 10%;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 115px;">招募可提佣金</label>
                        <div class="layui-input-inline">
                            <input type="number" name="directBrokerage" autocomplete="off" class="layui-input" min="0">
                        </div>
                        <label class="layui-form-mid">每人</label>
                    </div>
                </div>

                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline" style="width: 200px;">
                            每招募1个众筹成功人员可获得的佣金
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 10%;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 115px;">下级招募可提佣金</label>
                        <div class="layui-input-inline">
                            <input type="number" name="indirectBrokerage" autocomplete="off" class="layui-input" min="0">
                        </div>
                        <label class="layui-form-mid">每人</label>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline" style="width: 200px;">
                            每招募一个下级合伙人，下级合伙人每招募1个众筹成功人员，该合伙人可获得的佣金
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label">提现说明</label>
                        <div class="layui-input-inline" style="width: 200px;">
                            <textarea class="layui-textarea" name="remarks"></textarea>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>
<script type="text/javascript">
    var form = null;
    txz.initHeader({
        nav: [{
            name: '课程管理',
            curr: true
        }],
        info: '<span class="f12">共<b>${page.totalCount}</b>条记录',
        btns: [{
            name: '发布课程',
            icon: 'add',
            href: '${ctx}/activity/course/courseForm.do'
        }]
    });
    $(function () {
        // 加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });

        layui.use(['element', 'laydate', 'form'], function () {
            var laydate = layui.laydate, element = layui.element;
            form = layui.form;
            element.on('tab(type)', function (data) {
                if (data.index == 0) {
                    location.href = "${ctx}/activity/course/courseList.do";
                }
                else if (data.index == 1) {
                    location.href = "${ctx}/activity/course/listForDistribution.do";
                }
            });

            // 查询开始日期
            var start = {
                elem: 'input[name=startBegin]'
            };

            var end = {
                elem: 'input[name=startEnd]'
            };
            laydate.render(start);
            laydate.render(end);

            // 查询发布日期
            var start = {
                elem: 'input[name=createStart]'
            };

            var end = {
                elem: 'input[name=createEnd]'
            };
            laydate.render(start);
            laydate.render(end);

            form.on('radio(isDistribution)', function(data){
                var value = $(this).val();
                if (value == 0)
                    $('#distributionForm .layui-form-item:gt(0)').hide();
                else if (value == 1)
                    $('#distributionForm .layui-form-item:gt(0)').show();
                layer.style(checkIndex, {
                    width: '550px',
                    height: value == 0 ? '200px' : '550px',
                    top: value == 0 ? '40%' : '25%'
                });
            });
        });
    })

    /**
     * 设置当前活动的分销属性
     * @param actId
     */
    function setDistribution(actId, disCount) {
        if (actId == '' || actId == undefined) {
            layer.msg('活动参数传入有误', {
                icon: 2,
                title: '系统提示',
                time: 2000
            });
        } else {//展开属性弹窗
            $.post("${ctx}/activity/activity/getDistribution.do", {activityId: actId}, function (resp) {
                var disValue = undefined;
                if (resp.success) {
                    var data = resp.data;
                    disValue = data.isDistribution;
                    $("#distributionDiv").find('[name=isDistribution]').removeAttr('checked');
                    $("#distributionDiv").find('[name=isDistribution][value=' + disValue + ']').prop('checked', true);
                    if (data.directBrokerage > 0) {
                        $("#distributionDiv").find('[name=directBrokerage]').val(data.directBrokerage);
                    }
                    if (data.indirectBrokerage > 0) {
                        $("#distributionDiv").find('[name=indirectBrokerage]').val(data.indirectBrokerage);
                    }
                    $("#distributionDiv").find('[name=remarks]').val(data.remarks);
                    if (disValue == 0) {
                        $('#distributionForm .layui-form-item:gt(0)').hide();
                    } else if (disValue == 1) {
                        $('#distributionForm .layui-form-item:gt(0)').show();
                    }
                    if (disCount > 0) {
                        $("#distributionDiv").find('[name=isDistribution][value="0"]').attr('disabled', 'disabled');
                    } else {
                        $("#distributionDiv").find('[name=isDistribution][value="0"]').removeAttr('disabled');
                    }
                    form.render();
                } else {
                    $("#distributionDiv").find('[name=isDistribution]').removeAttr('checked');
                    $("#distributionDiv").find('[name=directBrokerage]').val('');
                    $("#distributionDiv").find('[name=indirectBrokerage]').val('');
                    $("#distributionDiv").find('[name=remarks]').val('');
                }
                form.render(null, 'distribution');
                $("#distributionDiv #distributionForm [name=activityId]").val(actId);
                checkIndex = layer.open({
                    type: 1,
                    area: ['550px', disValue == 1 || disValue == undefined ? '560px' : '200px'],
                    title: '设置当前活动的分销属性',
                    content: $("#distributionDiv"),
                    resize: false,
                    btn: ['确定', '取消'],
                    yes: function () { // 确定按钮
                        var isDistribution = $(':checked[name=isDistribution]').val();
                        var directBrokerage = $('[name=directBrokerage]').val();
                        var indirectBrokerage = $('[name=indirectBrokerage]').val();
                        if (isDistribution == undefined || isDistribution == '') {
                            util.layerMsgWarn('请选择分销状态');
                            return;
                        }
                        if (isDistribution == 1) {
                            if (directBrokerage == '') {
                                util.layerMsgWarn('请设置招募可提佣金');
                                return;
                            } else if (indirectBrokerage == '') {
                                util.layerMsgWarn('请设置下级招募可提佣金');
                                return;
                            } else if (parseFloat(directBrokerage) <= 0 || parseFloat(indirectBrokerage) <= 0) {
                                util.layerMsgWarn('佣金不能设置为0元和负数');
                                return;
                            }
                        }
                        if (parseFloat(directBrokerage) <= parseFloat(indirectBrokerage)) {
                            util.layerMsgWarn('下级佣金设置不合理');
                            return;
                        }
                        $.post($("#distributionDiv #distributionForm").attr('action'),
                            $("#distributionDiv #distributionForm").serialize(), function (res) {
                                if (res.success) {
                                    util.layerMsgSuccess('当前活动分销属性设置成功', function () {
                                        window.location.reload();
                                    });
                                } else {
                                    util.layerMsgError(res.description);
                                }
                            });
                    },
                    btn2: function () {//取消按钮
                        layer.close(checkIndex);
                    }
                });
            }, false);
        }
    }
</script>
</body>
</html>